#!/usr/bin/env python3
# coding: utf-8

# Copyright (C) 2018 Robert Griesel
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gio
from gi.repository import GLib

import sys
import gettext
import argparse
import os.path

from setzer.workspace.workspace import Workspace
import setzer.workspace.workspace_viewgtk as view
from setzer.app.service_locator import ServiceLocator
from setzer.dialogs.dialog_locator import DialogLocator


class MainApplicationController(Gtk.Application):

    def __init__(self):
        Gtk.Application.__init__(self, application_id='org.cvfosammmm.Setzer', flags=Gio.ApplicationFlags.HANDLES_OPEN)

    def do_startup(self):
        Gtk.Application.do_startup(self)
        self.is_active = False

        # setup gettext
        gettext.install('setzer', names=('ngettext',), localedir='@localedir_path@')

        # get settings
        self.settings = ServiceLocator.get_settings()
        dm_default = GLib.Variant.new_boolean(self.settings.get_value('preferences', 'prefer_dark_mode'))
        self.settings.gtksettings.set_property('gtk-application-prefer-dark-theme', dm_default)

        # init static variables
        ServiceLocator.set_setzer_version('@setzer_version@')
        ServiceLocator.set_resources_path('@resources_path@')
        ServiceLocator.set_app_icons_path('@app_icons_path@')
        resources_path = ServiceLocator.get_resources_path()
        app_icons_path = ServiceLocator.get_app_icons_path()
        Gtk.IconTheme.append_search_path(Gtk.IconTheme.get_default(), os.path.join(resources_path, 'icons'))
        for folder in ['arrows', 'greek_letters', 'misc_math', 'misc_text', 'operators', 'relations']:
            path = os.path.join(resources_path, 'symbols', folder)
            Gtk.IconTheme.append_search_path(Gtk.IconTheme.get_default(), path)
        Gtk.IconTheme.append_search_path(Gtk.IconTheme.get_default(), app_icons_path)

        # init main window, model, dialogs
        self.main_window = view.MainWindow(self)
        ServiceLocator.set_main_window(self.main_window)

    def do_open(self, files, number_of_files, hint=""):
        if not self.is_active:
            self.activate()
            self.is_active = True

        # open first session file if any
        for file in files:
            if file.get_path().endswith('.stzs'):
                filename = file.get_path()

                active_document = self.workspace.get_active_document()
                documents = self.workspace.get_all_documents()
                unsaved_documents = self.workspace.get_unsaved_documents()
                dialog = DialogLocator.get_dialog('close_confirmation')
                not_save_to_close_documents = dialog.run(unsaved_documents)['not_save_to_close_documents']

                if len(not_save_to_close_documents) == 0:
                    if documents != None:
                        for document in documents:
                            self.workspace.remove_document(document)
                    self.workspace.load_documents_from_session_file(filename)

        # open latex and bibtex files
        for file in files:
            if file.get_path().endswith('.tex') or file.get_path().endswith('.bib') or file.get_path().endswith('.cls') or file.get_path().endswith('.sty'):
                document_candidate = self.workspace.get_document_by_filename(file.get_path())
                if document_candidate != None:
                    self.workspace.set_active_document(document_candidate)
                else:
                    document_candidate = self.workspace.create_document_from_filename(file.get_path())
                    if document_candidate != None:
                        self.workspace.set_active_document(document_candidate)

    def do_activate(self):
        if not self.is_active:
            self.activate()
            self.is_active = True

    def activate(self):
        self.workspace = Workspace()
        ServiceLocator.set_workspace(self.workspace)
        ServiceLocator.init_autocomplete_provider(self.workspace)
        DialogLocator.init_dialogs(self.main_window, self.workspace)

        # init view
        if self.settings.get_value('window_state', 'is_maximized'):
            self.main_window.maximize()
        else: 
            self.main_window.unmaximize()
        self.main_window.set_default_size(self.settings.get_value('window_state', 'width'), 
                                          self.settings.get_value('window_state', 'height'))
        self.main_window.current_width = self.settings.get_value('window_state', 'width')
        self.main_window.current_height = self.settings.get_value('window_state', 'height')
        self.first_window_state_event = True
        self.main_window.show_all()

        self.color_manager = ServiceLocator.get_color_manager()
        self.fg_color = self.color_manager.get_theme_color('theme_fg_color')
        self.bg_color = self.color_manager.get_theme_color('theme_bg_color')

        self.main_window.connect('size-allocate', self.on_window_size_allocate)
        self.main_window.connect('notify::is-maximized', self.on_window_maximize_event)
        self.main_window.connect('delete-event', self.on_window_close)
        self.main_window.connect('draw', self.on_window_draw)

        # init controller
        self.workspace.init_workspace_controller()
        self.workspace.actions.quit_action.connect('activate', self.on_quit_action)

    def on_window_size_allocate(self, main_window, window_size):
        ''' signal handler, update window size variables '''

        if not main_window.ismaximized:
            main_window.current_width, main_window.current_height = main_window.get_size()

    def on_window_maximize_event(self, main_window, state_event):
        ''' signal handler, update window state variables '''

        main_window.ismaximized = main_window.is_maximized()
        return False
    
    def on_window_draw(self, main_window, context):
        ''' check for theme changes, update sidebar, textviews '''

        fg_color = self.color_manager.get_theme_color('theme_fg_color')
        bg_color = self.color_manager.get_theme_color('theme_bg_color')
        if self.fg_color.red != fg_color.red or self.bg_color.red != bg_color.red:
            self.fg_color = fg_color
            self.bg_color = bg_color
            
            try: documents = self.workspace.open_documents
            except AttributeError: pass
            else:
                is_dark_mode = ServiceLocator.get_is_dark_mode()
                for document in documents:
                    document.set_dark_mode(is_dark_mode)
        return False

    def save_window_state(self):
        main_window = self.main_window
        self.settings.set_value('window_state', 'width', main_window.current_width)
        self.settings.set_value('window_state', 'height', main_window.current_height)
        self.settings.set_value('window_state', 'is_maximized', main_window.ismaximized)
        self.settings.set_value('window_state', 'show_symbols', self.workspace.show_symbols)
        self.settings.set_value('window_state', 'show_document_structure', self.workspace.show_document_structure)
        self.settings.set_value('window_state', 'sidebar_paned_position', main_window.sidebar_paned.target_position)
        self.settings.set_value('window_state', 'show_help', self.workspace.show_help)
        self.settings.set_value('window_state', 'show_preview', self.workspace.show_preview)
        self.settings.set_value('window_state', 'preview_paned_position', main_window.preview_paned.target_position)
        self.settings.set_value('window_state', 'show_build_log', self.workspace.show_build_log)
        self.settings.set_value('window_state', 'build_log_paned_position', main_window.build_log_paned.target_position)
        self.settings.pickle()

    def on_window_close(self, window=None, parameter=None):
        self.save_quit()
        return True

    def on_quit_action(self, action=None, parameter=None):
        self.save_quit()

    def save_quit(self):
        for document in self.workspace.open_documents: document.state_manager.save_document_state()

        documents = self.workspace.get_unsaved_documents()
        active_document = self.workspace.get_active_document()

        if documents == None or active_document == None or DialogLocator.get_dialog('close_confirmation').run(documents)['all_save_to_close']:
            self.save_window_state()
            self.workspace.save_to_disk()
            self.quit()


argparser = argparse.ArgumentParser(usage='%(prog)s [OPTION...] [FILE...]')
argparser.add_argument('-V', '--version', action='version', version='@setzer_version@')
argparser.add_argument('file', nargs='*', help=argparse.SUPPRESS)
argparser.parse_args()

main_controller = MainApplicationController()
exit_status = main_controller.run(sys.argv)
sys.exit(exit_status)
